package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.constant.enums.ReconciliationStatusEnum;
import com.yunyao.dao.mapper.BamuReconciliationMapper;
import com.yunyao.dao.model.BamuReconciliation;
import com.yunyao.dao.model.BamuSpReconciliation;
import com.yunyao.dao.service.bamu.IBamuReconciliationService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 对账表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuReconciliationServiceImpl extends ServiceImpl<BamuReconciliationMapper, BamuReconciliation> implements IBamuReconciliationService {

    /**
     * 根据ID列表查询已删除的对账单
     *
     * @param reconciliationIdList 对账单ID列表
     * @return 返回已删除的对账单列表如果ID列表为空，则返回空列表
     */
    @Override
    public List<BamuReconciliation> listByIdsDeleted(List<String> reconciliationIdList) {
        // 检查对账单ID列表是否为空
        if (CollectionUtils.isEmpty(reconciliationIdList)) {
            return List.of(); // 如果为空，则直接返回空列表
        }
        // 创建查询条件封装器
        LambdaQueryWrapper<BamuReconciliation> wrapper =
                new LambdaQueryWrapper<BamuReconciliation>().in(BamuReconciliation::getId,
                reconciliationIdList).eq(BamuReconciliation::getIsDeleted,
                        DataDeletedStatus.NO.getType());
        // 使用封装器查询对账单列表
        return list(wrapper);
    }

    @Override
    public BamuReconciliation getByIdNoDelete(String id) {
        // 验证ID有效性
        if (StringUtils.isBlank(id)) {
            return null;
        }
        // 构建查询条件，寻找ID为指定值且未被删除的采购合同记录
        LambdaQueryWrapper<BamuReconciliation> wrapper =
                new LambdaQueryWrapper<BamuReconciliation>()
                        .eq(BamuReconciliation::getId, id)
                        .eq(BamuReconciliation::getIsDeleted, DataDeletedStatus.NO.getType());
        // 执行查询并返回结果
        return getOne(wrapper);
    }


    @Override
    public void unBindSettlement(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return;
        }
        for (String id : idList) {
            LambdaUpdateWrapper<BamuReconciliation> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(BamuReconciliation::getId, id);
            wrapper.eq(BamuReconciliation::getStatus,
                    ReconciliationStatusEnum.PAYMENT_COLLECTION_IN_PROGRESS.getType());
            wrapper.eq(BamuReconciliation::getIsDeleted, DataDeletedStatus.NO.getType());
            wrapper.set(BamuReconciliation::getStatus,
                    ReconciliationStatusEnum.CLOUD_ACQUISITION_CONFIRMATION_COMPLETE.getType());
            update(wrapper);
        }
    }

    @Override
    public void bindSettlement(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return;
        }
        for (String id : idList) {
            LambdaUpdateWrapper<BamuReconciliation> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(BamuReconciliation::getId, id);
            wrapper.eq(BamuReconciliation::getStatus,
                    ReconciliationStatusEnum.CLOUD_ACQUISITION_CONFIRMATION_COMPLETE.getType());
            wrapper.eq(BamuReconciliation::getIsDeleted, DataDeletedStatus.NO.getType());
            wrapper.set(BamuReconciliation::getStatus,
                    ReconciliationStatusEnum.PAYMENT_COLLECTION_IN_PROGRESS.getType());
            update(wrapper);
        }
    }

    @Override
    public List<BamuReconciliation> listById(String Id) {
        // 检查合同ID是否为空或空白，如果是，则返回一个空列表
        if (StringUtils.isBlank(Id)) {
            return List.of();
        }
        // 创建一个LambdaQueryWrapper用于构建查询条件
        LambdaQueryWrapper<BamuReconciliation> wrapper =
                new LambdaQueryWrapper<BamuReconciliation>().eq(BamuReconciliation::getId,
                        Id).eq(BamuReconciliation::getIsDeleted, DataDeletedStatus.NO.getType());
        // 使用构建的查询条件获取并返回关联的材料列表
        return list(wrapper);
    }
}
